<script setup>
import { Lock, User } from '@element-plus/icons'
import { ref, watch } from 'vue'
import { register, login } from '@/api/user'
import { useUser } from '@/stores/modules/user.js'
import { useRouter } from 'vue-router'
const ruleForm = ref({ username: '', password: '', repassword: '' })
const ifregister = ref(0)
watch(ruleForm.value, (newvalue) => {
  console.log(newvalue)
})
const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 5, max: 10, message: '用户名必须是5~10为字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    {
      pattern: /^\S{6,15}$/,
      message: '密码必须是6~15的非空字符',
      trigger: 'blur'
    }
  ],
  repassword: [
    { required: true, message: '请再次输入密码', trigger: 'blur' },
    {
      pattern: /^\S{6,15}$/,
      message: '密码必须是6~15的非空字符',
      trigger: 'blur'
    },
    {
      validtor: (rule, value, callback) => {
        if ((value = !ruleForm.value.psd)) {
          callback(new Error('两次密码不一致'))
        } else {
          callback() //校验成功也必须写
        }
      }
    }
  ]
}
const form = ref(null)
const registerfn = async () => {
  await form.value.validate()
  //成功往下走，不超过提示校验失败信息
  await register(ruleForm.value)
  ElMessage.success('注册成功')
  //注册完，直接跳到登陆页面
  ifregister.value = 1
  console.log('校验成功')
}
const tiaozhuanfn = (a) => {
  ruleForm.value.username = ''
  ruleForm.value.password = ''
  ruleForm.value.repassword = ''
  ifregister.value = a
}
const user = useUser()
const router = useRouter()

const loginfn = async () => {
  await form.value.validate()
  const res = await login(ruleForm.value)
  console.log(res.data.token)
  user.gettoken(res.data.token)
  ElMessage.success('登录成功')
  console.log(router)
  router.push('/')
}
</script>
<template>
  <div class="containerlogin">
    <el-row>
      <el-col :span="12">
        <div class="img">
          <img src="@/assets/logo2.png" alt="" />
        </div>
      </el-col>
      <el-col :span="6" :offset="3" class="loginarea">
        <el-form ref="form" v-if="ifregister" :rules="rules" :model="ruleForm">
          <el-form-item>
            <h1>注册</h1>
          </el-form-item>
          <el-form-item prop="username">
            <el-input
              v-model="ruleForm.username"
              placeholder="请输入用户名"
              :prefix-icon="User"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              v-model="ruleForm.password"
              placeholder="请输入密码"
              :prefix-icon="Lock"
              type="password"
            ></el-input>
          </el-form-item>
          <el-form-item prop="repassword">
            <el-input
              v-model="ruleForm.repassword"
              placeholder="请再次输入密码"
              :prefix-icon="Lock"
              type="password"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button
              @click="registerfn"
              color="rgb(0,170,250)"
              style="padding: 20px; width: 100%; color: white"
            >
              注册
            </el-button>
          </el-form-item>
          <el-form-item @click="tiaozhuanfn(0)">
            <span>《--返回</span>
          </el-form-item>
        </el-form>
        <el-form ref="form" v-else :rules="rules" :model="ruleForm">
          <el-form-item>
            <h1>登录</h1>
          </el-form-item>
          <el-form-item prop="username">
            <el-input
              v-model="ruleForm.username"
              placeholder="请输入用户名"
              :prefix-icon="User"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              v-model="ruleForm.password"
              placeholder="请输入密码"
              :prefix-icon="Lock"
              type="password"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <div class="jizhuorwjipsd">
              <el-checkbox>记住我</el-checkbox>
              <span>忘记密码</span>
            </div>
          </el-form-item>
          <el-form-item>
            <el-button
              @click="loginfn"
              color="rgb(0,170,250)"
              style="padding: 20px; width: 100%; color: white"
            >
              登录
            </el-button>
          </el-form-item>
          <el-form-item @click="tiaozhuanfn(1)"> -->注册 </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>
<style>
.jizhuorwjipsd {
  display: flex;
  justify-content: space-between;
  width: 100%;
}
.img {
  background-image: url('@/assets/login_bg.jpg');
  height: 100vh;
  background-size: 100% 100%;
}
.el-input__prefix-inner {
  padding: 15px;
}
.img img {
  padding: 25vh 10vw;
}
.loginarea {
  height: 50vh;
  margin-top: 25vh;
  padding: 40px 35px;
  background-color: white;
}
.containerlogin {
  background-color: white;
}
</style>
